{% extends "templates/atnet_base.html" %}

{% block csslink %}
{{ super() }}
<script src="/media/js/jquery.js"></script>

{% include 'templates/includes_datatable.html' %}
{% include 'templates/includes_jquery_ui.html' %}
<script src="/media/js/form_popup.js"></script>
	
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
	$('#invoices').dataTable({
		"bPaginate": true,
		"bLengthChange": false,
		"bFilter": true,
		"bSort": true,
		"bInfo": true,
		"bAutoWidth": false,
		"oLanguage": {
			"sUrl": "/media/js/dataTables.{{ app_user.lang }}.txt"
		},
		"bProcessing": true,
		"sAjaxSource": "{{ uri_for('vie_invoice', imm_id=immeuble.key().id(), command='list_4invoice_json') }}",
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			$('td:eq(0)', nRow).html(aData[1]);
			$('td:eq(1)', nRow).html(aData[0]);
			$('td:eq(2)', nRow).html(aData[6]);
			$('td:eq(3)', nRow).html(aData[7]);
			$('td:eq(4)', nRow).html('<nobr><a href="javascript:void(0);" id="' + aData[5] + '" class="edit">{{ _("Edit") }}</a> <a href="javascript:void(0);" id="' + aData[5] + '" class="delete">{{ _("Delete") }}</a></nobr>');
		},
		"aoColumnDefs": [{ "bVisible": false, "aTargets": [2] }, { "bVisible": false, "aTargets": [3] }]
	});
	
	$('#add-invoice').live('click', function(){
		$(this).LaunchPopup(
			{
				TableId: '#invoices',
				DivId: '#dialog-form',
				FormId: '#generated-form',
				UrlForm: "{{ uri_for('vie_invoice',imm_id=app_user.last_active_immeuble_id, command='add') }}",
				Mode: 'add',
				Title: '{{ _("Add invoice") }}',
			}
		)
	});
	
	$('#invoices tbody tr a').live('click', function(e) {
		if ($(this).attr('class') == 'edit') {
			$(this).LaunchPopup(
					{
						TableId: '#invoices',
						DivId: '#dialog-form',
						FormId: '#generated-form',
						UrlForm: "{{ uri_for('vie_invoice', imm_id=app_user.last_active_immeuble_id, command='edit') }}" + e.target.id + "/",
						Mode: 'edit',
						Title: '{{ _("Edit invoice") }}',
					}
				)
		}
		if ($(this).attr('class') == 'delete') {
			$(this).LaunchPopup(
					{
						TableId: '#invoices',
						DivId: '#dialog-form',
						FormId: '#generated-form',
						UrlForm: "{{ uri_for('vie_invoice', imm_id=immeuble.key().id(), command='delete') }}" + e.target.id + "/",
						Mode: 'delete',
						Title: '{{ _("Delete invoice") }}',
					}
				)
		}
	});
});
</script>
{% endblock %}

{% block title %} 
	{{ title }}
{% endblock %}

{% block header %}
{{ header }}
{% endblock %}

{% block login%}
{% if show_login %}
{{ super() }}
{% endif %}
{% endblock%}

{% block navigation %}
{% if not show_login %}
{{ super() }}
{% endif %}
{% endblock %}

{% block page_name %}
{{ content_header }}
{% endblock %}

{% block section_menu %}
{% include 'templates/includes_vie_de_la_copropriete.html' %}
{% endblock %}

{% block main_content %}
<p>
	<p><a href="javascript:void(0);" id="add-invoice">{{ _('Add invoice') }}</a></p>
	<div id="demo">
	<table cellpadding="0" cellspacing="0" border="0" class="display" id="invoices" width="100%">
		<thead>
			<tr>
				<th>{{ _(fields.invoice_date) }}</th>
				<th>{{ _(fields.supplier)  }} </th>
				<th></th>
				<th></th>
				<th>{{ _(fields.title) }}</th>
				<th>{{ _(fields.amount) }} </th>
				<th width="20"></th>
			</tr>
		</thead>
		<tbody>
		</tbody>
		<tfoot>
			<tr>
				<th>{{ _(fields.invoice_date) }}</th>
				<th>{{ _(fields.supplier)  }} </th>
				<th></th>
				<th></th>
				<th>{{ _(fields.title) }}</th>
				<th>{{ _(fields.amount) }} </th>
				<th></th>
			</tr>
		</tfoot>
	</table>
	</div>
	{% include 'templates/includes_popup_form.html' %}
</p>
<script>
$(document).ready(function() {
	var user_lang = "{{ app_user.lang }}";
	if (user_lang == "en") { user_lang = "en-GB"};
	$.datepicker.setDefaults( $.datepicker.regional[ user_lang ] );
	
	$("#invoice_date").live("focus", function() {
	    $(this).datepicker({
	        inline: true,
	    });
	});
	$("#deadline").live("focus", function() {
	    $(this).datepicker({
	        inline: true 
	    });
	});
	
	$.fn.recalcAmounts = function() {
		var amount = $("#amount").val();
		if ((amount.length > 0) && (amount == amount.replace(/[^0-9\.]/g, ''))) {
			$('#dialog-form').parent().find("button[id*=dialog-button-save]").attr('disabled', true);
			$.ajax({
				type: 'GET',
				dataType: 'json',
				url: "{{ uri_for('parametrage_nature_des_depenses', imm_id=immeuble.key().id(), command='calc_vat_charge_deduction') }}?expenditure=" + $("#expendature").val() + "&amount=" + $("#amount").val(),
				success: function(data) {
					$("#tva").val(data.vat_value);
					$("#rental_charges").val(data.rental_charges);
					$("#tax_deduction").val(data.tax_deduction);
				},
				error: function (error) {
					alert('error');
				}
			})
			$('#dialog-form').parent().find("button[id*=dialog-button-save]").attr('disabled', false);
		}
	}
	
	$("#amount").live("focusin", function() {
		if ($("#recalc_button").length == 0) {
			var btn = $('<button id="recalc_button">Calc taxes</button>');
			btn.insertAfter('#amount');
		}
	});
	
	$("#recalc_button").live("click", function() {
		$(this).recalcAmounts();
		return false;
	});
	
	var availableTagsSupl;
	availableTagsSupl = getMessages("{{ uri_for('vie_suppliers', imm_id=immeuble.key().id(), command='list_names_json') }}");
	$("#supplier").live("focus", function() {
		$(this).autocomplete({
			delay: 0,
			minLength: 0,
			source: availableTagsSupl
		});
		$(this).data("autocomplete").search($(this).val());
	});
	
	var availableTagsCK;
	availableTagsCK = getMessages("{{ uri_for('parametrage_cles', imm_id=immeuble.key().id(), command='list_names_json') }}");
	$("#charges_key").live("focus", function() {
		$(this).autocomplete({
			delay: 0,
			minLength: 0,
			source: availableTagsCK
		});
		$(this).data("autocomplete").search($(this).val());
	});
	
	var availableTagsExp;
	availableTagsExp = getMessages("{{ uri_for('parametrage_nature_des_depenses', imm_id=immeuble.key().id(), command='list_names_json') }}");
	$("#expendature").live("focus", function() {
		$(this).autocomplete({
			delay: 0,
			minLength: 0,
			source: availableTagsExp
		});
		$(this).data("autocomplete").search($(this).val());
	});
});
</script>			

{% endblock %}
